package com.hkfg.controller;

import com.hkfg.domain.Moment;
import com.hkfg.service.impl.MomentServiceImpl;
import com.hkfg.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@CrossOrigin
@ResponseBody
public class MomentController {
    @Autowired
    private MomentServiceImpl momentService;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 获得全部留言
     * @param id
     * @return
     */
    @RequestMapping(value = "/allmoment", method = RequestMethod.GET)
    public List<Moment> selectAll(String id){
        return momentService.selectUserMoment(Integer.parseInt(id));
    }

    /*新建留言*/
    @RequestMapping(value = "/buildmoment", method = RequestMethod.POST)
    public int buildMoment(@RequestBody Moment moment){
//        String content = request.getParameter("content");
//        String senderName = request.getParameter("senderId");
//        String receiverName = request.getParameter("receiverId");

//        int senderId = userService.getUserId(senderName);
//        int receiverId = userService.getUserId(receiverName);
//
//        Moment moment = new Moment();
//        moment.setContent(content);
//        moment.setSenderID(senderId);
//        moment.setReceiverID(receiverId);
        System.out.println("开始新建留言");

        if (momentService.buildMoment(moment)>0){
            return 1;
        }
        else {
            return 0;
        }
    }

    /**
     * 修改留言
     * @param moment
     * @return
     */
//    @RequestMapping(value = "/modifymoment", method = RequestMethod.POST)     //
//    public int modifymoment(String id, String content){
//        System.out.println(id);
//        return momentService.modifyMoment(Integer.parseInt(id), content);
//    }
    @RequestMapping(value = "/modifymoment", method = RequestMethod.POST)     //
    public int modifymoment(@RequestBody Moment moment){
//        System.out.println(id);
        return momentService.modifyMoment(moment.getId(), moment.getContent());
    }

    /*删留言*/
    @RequestMapping(value = "/deletemoment", method = RequestMethod.DELETE)
    public int deleteMomentById(HttpServletRequest request){
        Integer momentId = Integer.parseInt(request.getParameter("id"));

        return momentService.deleteMomentById(momentId);
    }


}















